// 搜索展示界面

const app = getApp()
const db = wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsObjects:[],
    searchText: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getText(options)
    this.getGoods()
  },

  //获取上一界面的参数：搜索框文本
  getText(options){
    var that = this;
    that.setData({
      searchText: options.text
    })
    console.log('text: ',that.data.searchText)
  },

  //匹配商品标题中含有搜索key的结果
  getGoods(){
    var that = this
    console.log('text: ',that.data.searchText)
    db.collection("goods").where({
      name: db.RegExp({
        regexp: that.data.searchText,
        options: 'i'
      })
    }).get().then(res=>{
      if(res.data.length==0){
        wx.redirectTo({
          url: '../../pages/chooseLib/chooseLib'
        })
        return
      }
      that.setData({
        goodsObjects : res.data,
      })
    })
  },

})